package com.atguigu.yygh.cmn.controller;


import com.atguigu.yygh.cmn.service.DictService;
import com.atguigu.yygh.common.R;
import com.atguigu.yygh.common.Result;
import com.atguigu.yygh.model.cmn.Dict;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * <p>
 * 组织架构表 前端控制器
 * </p>
 *
 * @author atguigu
 * @since 2021-12-08
 */
@RestController
@Api(description = "数据字典接口")
@RequestMapping("/admin/cmn/dict")
//@CrossOrigin
public class DictController {

    @Autowired
    private DictService dictService;

    @ApiOperation(value = "根据数据id查询子数据列表")
    @GetMapping("/findChildData/{id}")
    public R findChildData(@PathVariable Long id){

        List<Dict> allById = dictService.findAllById(id);

        return R.ok().data("list",allById);
    }

    @ApiOperation(value = "数据字典导出")
    @GetMapping("/exportData")
    public void exportData(HttpServletResponse response){
        dictService.exportData(response);
    }

    @ApiOperation(value = "数据字典导入")
    @PostMapping("/importData")
    public R importData(MultipartFile file){

        dictService.importDictData(file);

        return R.ok();
    }

    @ApiOperation(value = "获取数据字典名称")
    @GetMapping(value = "/getName/{parentDictCode}/{value}")
    public String getName(
            @PathVariable("parentDictCode") String parentDictCode,
            @PathVariable("value") String value) {

            String name = dictService.getNameByParentDictCodeAndValue(parentDictCode,value);
            return name;
    }

    @ApiOperation(value = "获取数据字典名称")
    @GetMapping(value = "/getName/{value}")
    public String getName(@PathVariable("value") String value){
        String name = dictService.getNameByParentDictCodeAndValue("",value);
        return name;
    }

    @ApiOperation(value = "查询所有的省份")
    @GetMapping("findByDictCode/{dictCode}")
    public R findByDictCode(@PathVariable String dictCode){

        List<Dict> list = dictService.findByDictCode(dictCode);

        return R.ok().data("list",list);
    }

    @ApiOperation(value = "查询单个省下面的市区")
    @GetMapping("findChildDataa/{id}")
    public R findChildDataa(@PathVariable Long id){
        List<Dict> list = dictService.findChildDataa(id);

        return R.ok().data("dictList",list);
    }
}

